package blog.controller;

import blog.entity.Consumer;
import blog.entity.Document;
import blog.service.back.DocumentBackService;
import blog.util.back.ControllerUtil;
import blog.vo.back.*;
import blog.vo.common.PaginationVO;
import blog.vo.common.ResponseListObjectVO;
import blog.vo.common.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * 　　* @description: TODO
 * 　　* @param
 * 　　* @return
 * 　　* @throws
 * 　　* @author Administrator
 * 　　* @date $ $
 *
 */
@Controller
@RequestMapping("/document")
public class DocumentController {
    @Autowired
    private DocumentBackService documentBackService;

    @RequestMapping("/get-all-document")
    public String getAllDocument(PaginationVO paginationVO, Model model){
        List<DocumentVO> allDocument = documentBackService.getAllDocument(paginationVO);
        model.addAttribute("allDocument",new ResponseListObjectVO<DocumentVO>(200,"ok",allDocument).getResponseObject());
        return "home";
    }

    @PostMapping("/by-document-status-get-all-document")
    public String byDocumentStatusGetAllDocument(ByDocumentStatusGetAllDocumentVO byDocumentStatusGetAllDocumentVO, Model model){
        List<DocumentVO> allDocument = documentBackService.byDocumentStatusGetAllDocument(byDocumentStatusGetAllDocumentVO);
        model.addAttribute("allDocument",new ResponseListObjectVO<DocumentVO>(200,"ok",allDocument).getResponseObject());
        return "home";
    }

    @PostMapping("/update-document-status")
    public String updateDocumentStatus(UpdateDocumentStatusVO updateDocumentStatusVO,Model model){
        int result = documentBackService.updateDocumentStatus(updateDocumentStatusVO);
        ControllerUtil.updateResultUrl(result, "redirect:/document/get-all-document", model);
        return "redirect:/document/get-all-document";
    }

    @PostMapping("/add-document")
    public String addDocument(MultipartFile myFile, HttpSession session){
        try {
            String courseFile =  "C:\\Users\\Administrator\\IdeaProjects\\blog\\blog-back-controller\\target\\classes\\public\\consumer-head\\";
            Path path = Paths.get(courseFile, myFile.getOriginalFilename());
            myFile.transferTo(path);

            AddDocumentVO addDocumentVO = new AddDocumentVO();
            addDocumentVO.setDocumentName(myFile.getOriginalFilename());
            addDocumentVO.setDocumentPath("/static/document/" + myFile.getOriginalFilename());
            addDocumentVO.setDocumentSize(myFile.getSize() / 1024);

            ResponseVO<Consumer> consumer = (ResponseVO<Consumer>) session.getAttribute("consumer");
            addDocumentVO.setDocumentConsumerUuid(consumer.getResponseObject().getConsumerUuid());

            String blogUuid = (String) session.getAttribute("blogUuid");
            addDocumentVO.setDocumentBlogUuid(blogUuid);
            documentBackService.addDocument(addDocumentVO);
            session.setAttribute("blogUuid",null);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        return "redirect:/blog/get-all-highlight-blogInfo";
    }

}
